package cn.gson.boot.model.service;

import cn.gson.boot.model.dao.GongNengDao;
import cn.gson.boot.model.dao.RoleDao;
import cn.gson.boot.model.dao.RoleGongDao;
import cn.gson.boot.model.mapper.GongMapper;
import cn.gson.boot.model.mapper.RoleMapper;
import cn.gson.boot.model.pojos.Gong;
import cn.gson.boot.model.pojos.Role;
import cn.gson.boot.model.pojos.RoleGong;
import cn.gson.boot.model.pojos.Sort;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class RoleService {
    @Autowired
    RoleMapper roleMapper;
    @Autowired
    GongMapper gongMapper;
    @Autowired
    RoleDao roleDao;

    @Autowired
    RoleGongDao roleGongDao;


    //查询角色列表
    public List<Role> roleList(String searchOne){
        return roleMapper.roleList(searchOne);
    }

    //新增角色
    public void saveRole(Role role){
        roleDao.save(role);
    }

    //查询功能组列表
    public List<Gong> gongList2(Integer deptId){
       return gongMapper.gongList2(deptId);
    }

    public List<Integer> roleFunList(Integer jueId){
        return gongMapper.roleFunList(jueId);
    }

    //确定授权
    public void shouquan(Integer jueId,String gnList) {
        System.err.println("SPgnList:"+gnList.length());
        System.err.println("SPjueId:"+jueId);
        if(gnList.length()>0){
            String[] sp = gnList.split(",");
            //先删除此角色的所有功能
            roleGongDao.deletejuesegong(jueId);
            //再新增
            RoleGong rg = null;
            for (int i=0;i<sp.length;i++){
                System.err.println("sp:"+sp[i]);
                rg = new RoleGong(jueId,Integer.parseInt(sp[i]));
                roleGongDao.save(rg);
            }
        }else{
            roleGongDao.deletejuesegong(jueId);
        }
    }

    //查询功能组的名称（去重，根据功能名称分组）
    public List<String> gongnengzuName(){
        return gongMapper.gongnengzuName();
    }

    //根据功能组名称查询功能
    public List<Gong> gongnengList(String gnName){
        return gongMapper.gongnengList(gnName);
    }

    //查看所有的功能
    public List<Sort> selectAllGongNeng(String gnzName) {
        return gongMapper.selectAllGongNeng(gnzName);
    }

    //根据功能组名称查询此功能组所有的功能
    public List<Integer> selectGongNengBygnzuName(String gnzName) {
        return gongMapper.selectGongNengBygnzuName(gnzName);
    }

    @Autowired
    GongNengDao gongNengDao;
    //修改功能组的功能
    public void updateGnZuGn(String gnzName, String gnList) {
        String[] sp = gnList.split(",");

        //先把此功能组的所有功能移除掉，把sort_id设为null
        List<Integer> ss = gongMapper.selectGongNengBygnzuName(gnzName);
        for (int i=0;i<ss.size();i++){
            gongNengDao.updategnGnz(null,ss.get(i));
        }
        //根据功能组的名称来查询功能组的id
        Sort sort = gongMapper.selectGNZIDbygnzName(gnzName);
        Integer sortId = sort.getSortId();
        for (int i=0;i<sp.length;i++){
            gongNengDao.updategnGnz(sortId,Integer.parseInt(sp[i]));
        }
    }
}
